package acwing.前缀和与差分;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/11/21
 */
public class 激光炸弹 {

    static int n, m;
    static int N = 5010;
    static int[][] s = new int [N][N];
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException {

        int cnt, R;
        String[] init  = in.readLine().split(" ");
        cnt = Integer.parseInt(init[0]);
        R = Math.min(5001, Integer.parseInt(init[1]));
        n = m = 5001;

        while (cnt-- > 0) {

            String[] data = in.readLine().split(" ");
            int x = Integer.parseInt(data[0]) + 1;
            int y = Integer.parseInt(data[1]) + 1;
            int w = Integer.parseInt(data[2]);
            s[x][y] += w;

        }
        //求A的二维前缀和S S[i,j] = S[i - 1,j] + S[i,j - 1] - S[i - 1,j - 1] + A[i,j]
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {

                s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
            }
        }
        //遍历所有边长为R的形的前缀和数组，找最大的那一个
        //A[x,y] = S[i,j] - S[i - R,j] - S[i,j-R] + S[i - R,j - R]
        int res = 0;
        for (int i = R; i <= n; i++) {
            for (int j = R; j <= m; j++) {
                res = Math.max(res,s[i][j] - s[i - R][j] - s[i][j - R] + s[i - R][j - R]);
            }
        }
        System.out.println(res);
        in.close();
    }
}
